package com.ellricky.baselib.ui.reserve;


import static com.ellricky.baselib.http.Api.API_CT_RESERVE_DETAIL;
import static com.ellricky.baselib.http.Api.API_CT_RESERVE_SUBMIT;

import android.content.DialogInterface;
import android.view.View;
import android.widget.TextView;

import com.andr.common.permission.PermissionManager;
import com.andr.common.permission.Permissions;
import com.andr.common.util.DialUtil;
import com.andr.common.util.EmptyUtil;
import com.andr.common.util.StrUtil;
import com.andr.common.util.ToastUtil;
import com.ellricky.baselib.R;
import com.ellricky.baselib.dialog.picker.EPickerStrListener;
import com.ellricky.baselib.entity.reserve.CtReserveDetailEntity;
import com.ellricky.baselib.http.callback.ECallback;
import com.ellricky.baselib.listener.OnTakePhotoListener;
import com.ellricky.baselib.ui.base.BaseDetailActivity;
import com.ellricky.baselib.util.EDialogUtil;
import com.ellricky.baselib.widget.DetailItemView;
import com.flyco.roundview.RoundTextView;

import java.util.HashMap;
import java.util.Map;

public class CtReserveDetailActivity extends BaseDetailActivity implements EPickerStrListener {

    private TextView tvNameAge;

    private DetailItemView tvSchoolArea;
    private DetailItemView tvTel;
    private DetailItemView tvBirth;
    private DetailItemView tvReserveDate;
    private DetailItemView tvCtName;
    private DetailItemView tvPdName;
    private DetailItemView tvPdTime;

    private RoundTextView btnDial;

    private TextView tvReserveState;

    private String phoneNumber;


    @Override
    public int getContentLayout() {
        return R.layout.base_activity_ct_reserve_detail;
    }

    @Override
    public void initUIView() {
        super.initUIView();
        mTvTitle.setText(R.string.base_order_detail);

        tvNameAge = findViewById(R.id.tvNameAge);

        tvSchoolArea = findViewById(R.id.tvSchoolArea);
        tvTel = findViewById(R.id.tvTel);
        tvBirth = findViewById(R.id.tvBirth);
        tvReserveDate = findViewById(R.id.tvReserveDate);

        btnDial = findViewById(R.id.btnDial);
        tvReserveState = findViewById(R.id.tvReserveState);

        tvCtName = findViewById(R.id.tvCtName);
        tvPdName = findViewById(R.id.tvPdName);
        tvPdTime = findViewById(R.id.tvPdTime);

        btnDial.setOnClickListener(this);
        loadDetail();
    }


    private void loadDetail() {
        mPresenter.loadDetail(API_CT_RESERVE_DETAIL, id, new ECallback<CtReserveDetailEntity>(this) {
            @Override
            public void onSuccess(CtReserveDetailEntity data, String msg) {
                tvNameAge.setText(
                        String.format("%s %s",
                                StrUtil.filterEmpty(data.getName()), StrUtil.filterEmpty(data.getSexName())
                        )
                );

                tvSchoolArea.setValue(data.getAreaName());

                phoneNumber = StrUtil.filterEmpty(data.getTel());
                tvTel.setValue(phoneNumber);

                tvBirth.setValue(data.getBirthday());
                tvReserveDate.setValue(data.getAppointMarketDate());
                tvCtName.setValue(data.getMarkerName());
                tvPdName.setValue(data.getApproveName());
                tvPdTime.setValue(data.getAppointDate());

                // 1 = 待回访 2= 待排课 3= 已排课
                /*String appointStatus = StrUtil.filterEmpty(data.getAppointStatus());
                if ("1".equals(appointStatus)){
                    // 提交时间
                    btnSubmit.setVisibility(View.VISIBLE);
                    btnDial.setVisibility(View.GONE);
                    tvReserveState.setVisibility(View.GONE);
                }else*/
                if (data.waitPk()){
                    // 显示待排课
                    btnDial.setVisibility(View.VISIBLE);
                    tvReserveState.setVisibility(View.VISIBLE);
                    tvReserveState.setText(data.getAppointStatusName());
                }else if (data.hadPk()){
                    // 拨打电话
                    btnDial.setVisibility(View.VISIBLE);
                    tvReserveState.setVisibility(View.GONE);
                }
            }
        });
    }

    @Override
    protected void onClickView(View v) {
        super.onClickView(v);
        if (v == btnDial){
            // 拨打电话
            PermissionManager.getInstance()
                    .setGrantResult(this)
                    .requestPermissions(this, Permissions.CALL_PHONE);
            return;
        }

    }


    @Override
    public void onSelectItem(final String content, int picPos, int pickType) {
        EDialogUtil.showReserveConfirmDialog(this, new OnTakePhotoListener() {
            @Override
            public void onClickItem(DialogInterface dialogInterface, int item) {
                postConfirmTime(StrUtil.filterEmpty(content));
            }
        });
    }

    private void postConfirmTime(String time) {

        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("id", id);
        paramMap.put("appointDate", time);

        mPresenter.sendBackRequest(API_CT_RESERVE_SUBMIT, paramMap);
    }

    @Override
    public void onGrantedAll() {
        super.onGrantedAll();
        if (EmptyUtil.isEmpty(phoneNumber)) {
            ToastUtil.show(this, "电话号码为空，无法拨打");
            return;
        }
        DialUtil.dial(this, phoneNumber);
    }

    @Override
    public void onDenied(String... permission) {
        super.onDenied(permission);
        if (Permissions.CALL_PHONE.equalsIgnoreCase(permission[0])) {
            ToastUtil.show(this, "请打开打电话权限");
        }
    }
}
